package com.hzw.service.impl;

import com.hzw.service.PhotoUploadService;
import com.hzw.utils.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/3/7.
 */
@Service
public class PhotoUploadServiceImpl implements PhotoUploadService {
    @Override
    public List<String> photoUpload(MultipartFile multipartFile, String userName) throws Exception{
        List<String> photoSrc = new ArrayList<String>();
        String savePath = "F:\\projectPhotos\\" + userName + "\\";     //定义每个用户图片保存在硬盘上文件夹的位置
        File folder = new File(savePath);
        if(!folder.exists()){                                         //创建文件夹
            folder.mkdirs();
        }
        String photoName = multipartFile.getOriginalFilename();     //获取完整图片名
        String ExtensionName = photoName.substring(photoName.indexOf("."), photoName.length() - 1); //获取图片后缀名
        String finalName = UUIDUtils.getUUID() + ExtensionName;     //生成uuid+后缀作为最终图片名
        try {
            multipartFile.transferTo(new File(savePath + finalName));
            photoSrc.add(savePath + finalName);
        } catch (IOException e) {
            throw new Exception("错误信息" + e.getMessage());
        }

        return photoSrc;
    }
}
